﻿


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图片工具</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin/style/admin.css" media="all">
    <style>
        ::-webkit-scrollbar {
            width: 12px !important;
            border-radius: 0px;
        }
    </style>
</head>
<body>

    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">图片工具</div>
                    <div class="layui-card-body" pad15>
                        <blockquote class="layui-elem-quote">
                            各种各样的图片工具，持续增加中。。。
                        </blockquote>
                        <div class="layui-card">
                            <div class="layui-card-body">
                                <div class="layui-tab layui-tab-brief">
                                    <ul class="layui-tab-title">
                                        <li class="layui-this">图片转Base64</li>
                                        <li>图片转格式</li>
                                        <li>标签3</li>
                                        <li>标签4</li>
                                        <li>标签5</li>
                                    </ul>
                                    <div class="layui-tab-content">
                                        <div class="layui-tab-item layui-show">
                                            <div class="layui-row">
                                                <div class="layui-col-xs4" style="text-align:center">
                                                    <input type="file" id="file-input">
                                                    <br/>
                                                    <img src="" style="width:200px; display:none;margin-top:5px" name="cImg" />
                                                </div>
                                                <div class="layui-col-xs8">
                                                    <div class="layui-btn-group">
                                                        <button class="layui-btn layui-btn-primary layui-btn-sm" id="dcopy"><i class="layui-icon layui-icon-file-b"></i>复制</button>
                                                        <button class="layui-btn layui-btn-primary layui-btn-sm" id="cleartxt"><i class="layui-icon layui-icon-fonts-clear"></i>清空</button>
                                                        <button class="layui-btn layui-btn-primary layui-btn-sm" id="deftxt"><i class="layui-icon layui-icon-fonts-code"></i>添加默认前缀</button>
                                                        <button class="layui-btn layui-btn-primary layui-btn-sm" id="base64ToImage">Base64转图片</button>
                                                    </div>
                                                    <textarea id="cBase64" placeholder="请输入base64图片编码" class="layui-textarea" rows="25" style="overflow-x: hidden; white-space: pre-wrap; margin-top: 5px"></textarea>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="layui-tab-item">
                                            <input type="text" name="cIcon" class="layui-input layui-hide">
                                            <img src="" style="width:200px; display:none;" name="cImgUrl" />
                                            <button type="button" class="layui-btn" id="Iconfile">
                                                <i class="layui-icon">&#xe67c;</i>上传图片
                                            </button>
                                        </div>
                                        <div class="layui-tab-item">内容-3</div>
                                        <div class="layui-tab-item">内容-4</div>
                                        <div class="layui-tab-item">内容-5</div>
                                    </div>
                                </div>


                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index'], function () {
            var $ = layui.$,
                admin = layui.admin,
                upload = layui.upload;

            upload.render({
                elem: '#Iconfile',
                url: "/api/Upload/Upload?type=TEMP",
                done: function (res) {
                    if (res.code == 200) {
                        $("img[name='cImgUrl']").attr("src", $.GetPathUri() + res.data);
                        $("img[name='cImgUrl']").show();
                        $("#Iconfile").hide();;
                        $("input[name='cIcon']").val(res.data);
                    } else {
                        layer.msg('上传失败!\n' + res.info, { icon: 2, time: 2000 });
                    }
                }
            });
            $("img[name='cImgUrl']").on('click', function () {
                $("#Iconfile").click();
            });
            function ImgToBase64() {
                var path = $("input[name='cIcon']").val();
                if (path == "") {
                    layer.msg('请先上传图片!', { icon: 0, time: 1000 });
                } else {
                    admin.req({
                        type: 'post',
                        url: '/api/ImageTool/ImgToBase64',
                        data: { path },
                        success: function (res) {
                            $("#cBase64").val(res.data);
                        }
                    })
                }
            }



            // 创建一个 FileReader 对象
            var reader = new FileReader();
            // 读取文件并将其内容转换为 DataURL（Base64）格式
            reader.onloadend = function () {
                // 获取到转换后的 base64 数据
                var base64Data = reader.result;
                $("#cBase64").val(base64Data);
            };
            // 选择要上传的图片文件
            var fileInput = document.getElementById('file-input');
            // 当文件被选中时触发事件
            fileInput.addEventListener('change', function () {
                // 通过 FileReader 对象读取文件
                reader.readAsDataURL(this.files[0]);
            });

            $("#base64ToImage").click(function () {
                $("img[name='cImg']").attr("src", $("#cBase64").val());
                $("img[name='cImg']").show();
            });
            $("#dcopy").click(function () {
                let textareaEl = document.querySelector("#cBase64")
                textareaEl.select();
                document.execCommand('copy');
                layui.layer.msg("复制成功");
            });
            $("#cleartxt").click(function () {
                $("#cBase64").val("");
            });
            $("#deftxt").click(function () {
                let base64 = $("#cBase64").val();
                if (base64.startsWith("data:image")) {
                    $("#cBase64").val(base64);
                } else {
                    $("#cBase64").val("data:image/jpg;base64," + base64);
                }
            });

            
        });
    </script>
</body>
</html>
